package com.situ.vivamarket.controller.sys;

import com.situ.vivamarket.model.Category;
import com.situ.vivamarket.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("/vm/sys/category")
public class CategoryController {
    private CategoryService categoryService;
    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("/list")
    public String list() {
        return "sys/category/list";
    }
    @GetMapping("/add")
    public String add(Integer parentId,Map<String, Object> map) {
        if(parentId != null){
            Category parentCat = categoryService.findById(parentId);
            if(parentCat != null){
                map.put("parentCat", parentCat);
            }
        }
        return "sys/category/add";
    }
    @GetMapping("/edit")
    public String edit(Integer id, Map<String,Object> map){
        if(id == null){
            map.put("error","要修改的类别编号为空");
            return "sys/category/edit";
        }
        Category category = categoryService.findById(id);
        if(category == null){
            map.put("error","修改的类别不存在");
        }else{
            map.put("category",category);
            map.put("parentCat",category.getParent());
        }
        return "sys/category/edit";
    }
}
